import axios from "axios";
import qs from 'qs'
if (process.env.NODE_ENV=='development'){
    axios.defaults.baseURL='http://localhost:9998/'
}
if (process.env.NODE_ENV=='production'){
    axios.defaults.baseURL='http://localhost:9998/'
}
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.timeout=5000;
axios.interceptors.request.use(
     config=>{
        config.headers={DeviceType:'H5'}//可每次发送请求之前的逻辑处理 比如判断token
         // if(config.method  === 'post'){
         //     config.data = qs.stringify(config.data);
         // }
        return config;
    }
 )
export function get(url,params) {
    return new Promise((resolve ,reject)=>{
      axios.get(url,{
          params:params
      }).then(res=>{
          resolve(res)
        }).catch(err=>{
            reject(err)
      })
    })
}
export function post(url,params) {
    console.log("-------------")
    console.log(qs.stringify(params))
    return new Promise((resolve, reject) => {
        axios.post(url, qs.stringify(params)).then(res=>{

            resolve(res)
        }).catch(err=>{
            reject(err)
        })
    })
}
export function getById(url,params) {

    return new Promise((resolve, reject) => {
        axios.get(url+"/"+params.id).then(res=>{
            resolve(res)
        }).catch(err=>{
            reject(err)
        })
    })
}
export function PostById(url,params) {

    return new Promise((resolve, reject) => {
        axios.post(url+"/"+params.id).then(res=>{
            resolve(res)
        }).catch(err=>{
            reject(err)
        })
    })
}